package com.zj.service.aop;

import com.zj.service.Iuser2DBService;
import com.zj.service.User;
import com.zj.service.User2DBService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component //将切面类声明为一个bean
@Aspect //声明这是一个切面类
@Order(10)
public class DeclareParentsAspect {

    @DeclareParents(value = "com.zj.service.IuserService+",defaultImpl= User2DBService.class)
    private Iuser2DBService iuser2DBService;
    //DeclareParents就是让被代理类IuserService具有本接口Iuser2DBService的能力，能力的默认实现为User2DBService。

    @Pointcut("execution(* com.zj.service.UserService.*(..))&& args(user)")
    public void pointcut(User user) {
    }

//如果pointcut方法是public的，可以直接在外部通知方法中引用。
 //   @Before("com.zj.service.aop.LogAnnotationAspect.pointcut2(user)&&this(iuser2DBService)")
    @Before("pointcut(user)&&this(iuser2DBService)")
    //before要想获取方法参数，需要在切点表达式中给出参数&& args(user)，且在切点方法pointcut中加上形参User
    public void Before(JoinPoint pj,User user,Iuser2DBService iuser2DBService) {
//        iuser2DBService.addUser2DB(user);
        long start = System.currentTimeMillis();
        System.out.println("DeclareParentsAspect.Before:" + pj.getSignature() + ", " + start);
    }





}
